package com.xinggq.pay.service.pay.channel.yse.pay.wxpay;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xinggq.pay.api.dto.YseCommonPayReqDTO;
import com.xinggq.pay.api.exception.PayException;
import com.xinggq.pay.service.pay.channel.yse.enums.YseMethodEnum;
import com.xinggq.pay.service.pay.channel.PayChannelPayApiService;
import com.xinggq.pay.service.adaptor.YseApiAdaptor;
import com.xinggq.pay.service.entity.YsePayConfigEntity;
import com.xinggq.pay.service.pay.channel.yse.enums.YsePayWayBindEnum;
import com.xinggq.pay.service.pay.channel.yse.pay.wxpay.dto.YseWxPayRespDto;
import com.xinggq.pay.service.pay.channel.yse.pay.wxpay.dto.YseWxPayResqDTO;
import com.xinggq.pay.service.service.YsePayConfigService;
import com.xinggq.pay.service.pay.channel.yse.utils.YseUtil;
import com.xinggq.pay.service.pay.channel.yse.pay.common.YsePayCommonDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 银盛微信支付实现类
 * @Description
 * @Author xinggq
 * @Date 2025/9/12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class YseWxPayServiceImpl implements
    PayChannelPayApiService<YseCommonPayReqDTO,YseWxPayRespDto> {

  private final YseApiAdaptor yseApiAdaptor;
  private final YsePayConfigService ysePayConfigService;

  @Override
  public YseWxPayRespDto pay(YseCommonPayReqDTO payParamDto) {
    Validator.validateNotNull(payParamDto, "银盛接口参数不能为空");
    if (!isWxPayWay(payParamDto)) {
      throw new PayException("不支持的银盛接口调用");
    }
    Validator.validateNotNull(payParamDto.getWechatParams(), " 微信支付信息不能为空");
    Validator.validateNotNull(payParamDto.getWechatParams().getWxOpenAppid(), " 微信公众号或小程序的appid不能为空");
    Validator.validateNotNull(payParamDto.getWechatParams().getSubOpenid(), " 用户在商户appid下的授权唯一标识不能为空");
    // 获取数据库中的银盛当前支付渠道配置
    YsePayConfigEntity ysePayConfig = ysePayConfigService.getConfigByMethod(YseMethodEnum.WECHAT_PAY);
    Validator.validateNotNull(ysePayConfig,
        String.format("银盛接口配置为空，支付方式:{%s}", YseMethodEnum.WECHAT_PAY.getMethod()));

    String requestData = buildWxReqDto(payParamDto, ysePayConfig);
    String respStr = yseApiAdaptor.pay(payParamDto.getTradeNo(),payParamDto.getChannelOutTradeNo(), requestData,
        YseMethodEnum.WECHAT_PAY.getGatewayUrl());
    return JSONObject.parseObject(respStr, YseWxPayRespDto.class);
  }

  /**
   * 构建微信支付参数，返回字符串
   */
  private String buildWxReqDto(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig){
    if (payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_MP.getPayWayEnum()){
      // 构建公众号参数
      return buildWpPayDto(payParamDto, ysePayConfig);
    }
    // 构建小程序参数
    return buildWiniPayDto(payParamDto, ysePayConfig);
  }



  /**
   * 构建公众号支付参数
   */
  private String buildWpPayDto(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig) {
    YseWxPayResqDTO wxPayReqDTO = YseWxPayResqDTO.buildYseMpWeChatPayResqDTO();
    return buildWxCommonDto(payParamDto, wxPayReqDTO, ysePayConfig);
  }


  /**
   *  构建小程序支付参数
   */
  private String buildWiniPayDto(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig) {
    YseWxPayResqDTO wxPayReqDTO = YseWxPayResqDTO.buildYseMiniWeChatPayResqDTO();
    return buildWxCommonDto(payParamDto, wxPayReqDTO, ysePayConfig);
  }


  /**
   * 构建微信支付通用参数
   */
  private String buildWxCommonDto(YseCommonPayReqDTO payParamDto, YseWxPayResqDTO wxPayReqDTO,
      YsePayConfigEntity ysePayConfig) {
    wxPayReqDTO.setSellerId(ysePayConfig.getSellerId());
    wxPayReqDTO.setSubOpenid(payParamDto.getWechatParams().getSubOpenid());
    wxPayReqDTO.setAppid(payParamDto.getWechatParams().getWxOpenAppid());
    wxPayReqDTO.setBusinessCode(ysePayConfig.getBusinessCode());
    wxPayReqDTO.setOutTradeNo(payParamDto.getChannelOutTradeNo());
    wxPayReqDTO.setSubject(payParamDto.getSubject());
    wxPayReqDTO.setTotalAmount(payParamDto.getTotalAmount());
    return buildWithPayCommonDto(ysePayConfig,
        JSONUtil.toJsonStr(BeanUtil.beanToMap(wxPayReqDTO, true, true)));
  }


  /**
   * 构建通用参数
   */
  private String buildWithPayCommonDto(YsePayConfigEntity ysePayConfig, String bizContent) {
    YsePayCommonDTO ysePayCommonDTO = YsePayCommonDTO.buildYseCommonPayDto(ysePayConfig, bizContent,YseMethodEnum.WECHAT_PAY);
    return YseUtil.mapToSortedString(BeanUtil.beanToMap(ysePayCommonDTO, true, true));
  }


  private boolean isWxPayWay(YseCommonPayReqDTO payParamDto) {
    if (payParamDto.getYsePayWay() == null) {
      // 默认微信小程序支付
      payParamDto.setYsePayWay(YsePayWayBindEnum.YSE_MINI.getPayWayEnum());
      return true;
    }
    return payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_MP.getPayWayEnum()
        || payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_MINI.getPayWayEnum();
  }


}
